<?php namespace App\Admin\Access\User;

//基础条件
use App\Core\Repository\Criterias\FixedCriteria;
use App\Core\Repository\Criterias\NotDeleteCriteria;
use App\Core\Repository\Criterias\PaginateCriteria;
//过滤器
use App\Core\Repository\Criterias\Filters\InputCriteria;
use App\Core\Repository\Criterias\Filters\InputKeywordsCriteria;
use App\Core\Repository\Criterias\Filters\InputTimeRangeCriteria;

trait Service {
    
    //基础服务
    use \App\Core\Service\Service;
    use \App\Core\Service\Services\Indexer;
    use \App\Core\Service\Services\Creater;
    use \App\Core\Service\Services\Updater;
    use \App\Core\Service\Services\Changer;
    use \App\Core\Service\Services\Deleter;
    use \App\Core\Service\Services\Detailer;
    //操作日志
    use \App\Admin\Base\LogService;
    //服务
    use Services\StatusManage;
    use Services\ResetPassword;
    use Services\RoleManage;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new Repository);
        $this->setMeta(new Meta);
        $this->repository = $this->getRepository();
        $this->repository->addCriteria(new NotDeleteCriteria(), '', true);//未删除
        $this->repository->addCriteria(new FixedCriteria('type', '0'), '', true);//类型
        $this->repository->addCriteria(new Criterias\HasPermission(), '', true);//权限
        //添加观察者
        $this->addListeners([
            'create' => new Listeners\CreateListener, 
            'update' => new Listeners\UpdateListener
        ]);
    }
	
    //列表
    public function lists(){
        //过滤器
        $conditions = [
            new InputKeywordsCriteria('username,nickname,name,phone,email'),
            new InputTimeRangeCriteria('created_at'),
            new InputCriteria('status')
        ];
        $count = $this->repository->addCriterias($conditions)->count();
        $conditions[] = new PaginateCriteria();
        $data = $this->repository->resetQuery()->resetCriteria()->addCriterias($conditions)->all();
        return $this->success([
            'count' => $count,
            'data' => $data
        ]);
    }
    
    //销售主管
    public function sellerManagers(){
        //过滤器
        $conditions = [
            new FixedCriteria('role_id', '1010')
        ];
        $data = $this->repository->addCriterias($conditions)->all();
        return $this->success([
            'data' => $data
        ]);
    }
}